package shmged.com.shlcm.reimbursement;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONObject;

import shmged.com.shlcm.fc.CmPersistenceHelper;
import shmged.com.shlcm.fc.CmQueryResult;
import shmged.com.shlcm.fc.CmQuerySpec;
import shmged.com.shlcm.objs.persist.CmReimbursementDetailEntry;
import shmged.com.shlcm.utils.CmDateUtil;
import shmged.com.shlcm.utils.CmIBAHelper;
import shmged.com.shlcm.utils.CmWTUserUtil;
import wt.content.ApplicationData;
import wt.content.ContentHelper;
import wt.content.ContentRoleType;
import wt.doc.WTDocument;
import wt.fc.QueryResult;
import wt.fc.ReferenceFactory;
import wt.org.WTUser;
import wt.util.WTException;

public class CmShowPartHelper {
	private CmIBAHelper cmIBAHelper;
	private WTDocument wtdoc=null;
	private String oid=null;
	
	public WTDocument getDoc(){
		return this.wtdoc;
	}
	public CmShowPartHelper(String oid) throws Exception {
		ReferenceFactory rf = new ReferenceFactory();
		WTDocument pn = (WTDocument)rf.getReference(oid).getObject();
		if(null==pn){
			
		}else{
			this.oid=oid;
			wtdoc=(WTDocument) pn;
			cmIBAHelper=new CmIBAHelper(pn);
		}
	}
	public String getIBAValue(String ibaName){
		if(cmIBAHelper!=null){
			String result=cmIBAHelper.getIBAValue(ibaName);
			if(null!=result){
				return result;
			}else{
				return "";
			}
		}else{
			return "";
		}
	}
	
	public List getPrimarys(){
		if(null==wtdoc){
			return new ArrayList();
		}
		List list=this.getSecondaryFile(this.wtdoc);
		
		return list;
	}
	
	public static String getPrimarys2(String oid) throws Exception{
		
		ReferenceFactory rf=new ReferenceFactory();
		
		JSONObject retJson = new JSONObject();
		JSONArray jsonArr = new JSONArray();
		retJson.put("rows", jsonArr);
		
		List primarys=new ArrayList();
		JSONObject item=null;
		CmShowPartHelper cm=new CmShowPartHelper(oid);
		List list=cm.getPrimarys();
		
		if(null!=list){
			for (int i = 0; i < list.size(); i++) {
				item=new JSONObject();
				Set<String> keyset=((HashMap)(list.get(i))).keySet();
				Iterator<String> it=keyset.iterator();
				while (it.hasNext()) {
					String temp=it.next();
					item.put(temp, ((HashMap)(list.get(i))).get(temp));
				}
				primarys.add(item);
			}
		}
		for (int i = 0; i < primarys.size(); i++) {
			jsonArr.put(primarys.get(i));
		}
		return retJson.toString();
	} 	

	public String getUserName(){
		String perno=this.getIBAValue("PersonalNo");
		if(null!=perno&&perno.trim().length()!=0){
			WTUser wtUser=CmWTUserUtil.getWTUserByName(perno);
			return wtUser.getFullName();
		}else{
			return "";
		}
	}
	
	public List getCmReimbursementDetailEntry() throws Exception{
		List list=new ArrayList<CmReimbursementDetailEntry>();
		
		if(null==oid||oid.trim().length()==0){
			return list;
			}
		
		CmQuerySpec qs=new CmQuerySpec(CmReimbursementDetailEntry.class);
		//qs.appendWhere(CmReimbursementDetailEntry.CMIDMASTER, SearchCondition.EQUAL, oid);
		CmQueryResult qr=CmPersistenceHelper.manager.find(qs);
		while (qr.hasNext()) {
			Object obj=qr.next();
			list.add(obj);
		}
		return list;
	}
	
	public List getSecondaryFile(WTDocument doc)
	{
		SimpleDateFormat df = CmDateUtil.getDefaultSimpleDateFormat("yyyy-MM-dd hh:mm");
		List list=new ArrayList();
		
		try {
			 QueryResult qr =  ContentHelper.service.getContentsByRole(doc, ContentRoleType.SECONDARY);
			 while(qr.hasMoreElements())
				{
				 Map item=new HashMap();
					Object obj = (Object)qr.nextElement();
					if(obj instanceof ApplicationData)
					{
						ApplicationData appData = (ApplicationData)obj;
						item.put("id", appData.getFileName());
						item.put("name", appData.getFileName());
						item.put("size", Float.toString(appData.getFileSizeKB()) + "KB");
						item.put("ModifyTimes", df.format(appData.getModifyTimestamp()));
						item.put("Modify", appData.MODIFIED_BY);
						item.put("path", appData.getUploadedFromPath());
						list.add(item);
					}
				}
		} catch (WTException e) {
			e.printStackTrace();
		}
		return list;
	}
}
